//
//  674. 最长连续递增序列.swift
//  LeetCodeTrain
//
//  Created by rjb on 2021/6/25.
//  Copyright © 2021 rjb. All rights reserved.
//

import Foundation
class Solution674 {
    // 最长连续递增序列
    // 用贪心就可以
    func findLengthOfLCIS(_ nums: [Int]) -> Int {
        guard nums.count > 0 else {
            return 0
        }
        var result = 1
        var tmp = 1
        var pre = nums[0]
        for item in nums {
            if item > pre {
                tmp += 1
            } else {
                tmp = 1
            }
            pre = item
            result = max(result, tmp)
        }
        return result
    }
    // 用动态规划
    // 定义dp[i] 以i结尾的最长递增子序列
    // 如果nums[i] > nums[i - 1], dp[i] = dp[i - 1] + 1
    // 如果nums[i] < nums[i - 1], dp[i] = 1
    func findLengthOfLCIS2(_ nums: [Int]) -> Int {
        var dp = Array(repeating: 1, count: nums.count)
        var result = 1
        for i in 1..<nums.count {
            if nums[i] > nums[i - 1] {
                dp[i] = dp[i - 1] + 1
            } else {
                dp[i] = 1
            }
            result = max(result, dp[i])
        }
        return result
    }
    static func test() {
        let nums1 = [10, 9, 2, 5, 3, 7, 101, 18]
        let nums2 = [0,1,0,3,2,3]
        let nums3 = [7,7,7,7,7,7,7]
        let num4 = [4,10,4,3,8,9]
        
        let num5 = [2,2,2,2,2]
        let result = Solution674().findLengthOfLCIS2(nums1)
        print(result)
    }
}
